package com.chezhujie.www.service;

import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.enums.OrderStatus;
import com.chezhujie.www.persistence.CzjstatisticalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.xml.transform.Result;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/8/4.
 */
@Service
public class CzjstatisticalService {
    @Autowired
    private CzjstatisticalMapper czjstatisticalMapper;


    public Pagination orderList(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjstatisticalMapper.getOrderTotalRows(params);
        List<Map<String, Object>> list = czjstatisticalMapper.getOrderList(params);
        pagination.setTotalCount(count);

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).get("orderType")==null){

            }else{
                list.get(i).put("orderStatusName",OrderStatus.getName((Integer)list.get(i).get("orderType")));
            }
        }
        pagination.setList(list);


        return pagination;
    }

    public Pagination activityList(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjstatisticalMapper.getActivityTotalRows(params);
        List<Map<String, Object>> list = czjstatisticalMapper.getActivityList(params);
        pagination.setTotalCount(count);

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).get("orderType")==null){

            }else{
                list.get(i).put("orderStatusName",OrderStatus.getName((Integer)list.get(i).get("orderType")));
            }
        }
        pagination.setList(list);


        return pagination;
    }
    public Pagination commodityList(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjstatisticalMapper.getCommodityTotalRows(params);
        List<Map<String, Object>> list = czjstatisticalMapper.getCommodityList(params);
        pagination.setTotalCount(count);

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).get("orderType")==null){

            }else{
                list.get(i).put("orderStatusName",OrderStatus.getName((Integer)list.get(i).get("orderType")));
            }
        }
        pagination.setList(list);


        return pagination;
    }


    public BigDecimal getTotalAmountByOrder(Map<String, Object> param) {
        Map<String,Object> result = czjstatisticalMapper.getTotalAmountByOrder(param);
        return (BigDecimal)result.get("totalAmount");
    }

    public BigDecimal getTotalAmountByActivity(Map<String, Object> param) {
        Map<String,Object> result = czjstatisticalMapper.getTotalAmountByActivity(param);
        return (BigDecimal)result.get("totalAmount");
    }
    public BigDecimal getTotalAmountByCommodity(Map<String, Object> param) {
        Map<String,Object> result = czjstatisticalMapper.getTotalAmountByCommodity(param);
        return (BigDecimal)result.get("totalAmount");
    }
}
